import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { HttpClient } from '@angular/common/http';
import { JsonResult } from 'src/app/model/JsonResult';

@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {

  validateForm: FormGroup;
  url = {
    'login':'localhost:8080/login'
  }

  submitForm(): void {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }

    const username:string = this.validateForm.controls.userName.value;
    const password:string = this.validateForm.controls.password.value;
    this.http.post(this.url.login,{"username":username,"password":password}).subscribe(
      ( jr:JsonResult )=>console.log(jr),error => console.log('登录失败')
    )

  }

  constructor(private fb: FormBuilder,private http:HttpClient) {}

  ngOnInit(): void {
    this.validateForm = this.fb.group({
      userName: [null, [Validators.required]],
      password: [null, [Validators.required]],
      remember: [true]
    });
  }




}
